/*
 * This file contains functions for the request of a project, for groups
 */

/*
 * this function displays one approved project that anyone can see
 */
function displayOneApprovedProject(projectId, notes, status, CComments) {
    $('body > section').empty();                    //empty the content of the section
    displayMeetingOneProject(projectId, notes, status, CComments);
}

/*
 * This function adds a request of a project to the users group
 */
function addApprovedProject(projectId, groupId) {
    var prioritet = "high";                             // sets the priority to high if its the first request
    var ourList = document.getElementById('ourProjects'); // get list of choosen projects
    var allListItems = ourList.getElementsByTagName('li');  // get all element of that list(all projects)
    if(allListItems.length == 1) {                          //if the project is number two in the list then set 
        prioritet = "medium";                               // priority to medium
    } else if (allListItems.length == 2) {                  // if the project is the last to be choosen then set
        prioritet = "low";                                  // priority to low
    }
    if(allListItems.length < 3){                            // because the group can only have 3 request, do this check
        $.ajax ({
            url : 'higface/student/library/addRequest.php',
            type : 'POST',
            data: {
                projectid: projectId,
                groupid: groupId,
                prio: prioritet
            },
            success : function (tmp) {
                data = eval ('('+tmp+')');
                if(data.error){                 // if something went wrong then display error and return
                    alert(data.error);
                    return;
                }                
                $('body > section').load ('pages/availableProjects.php');   // reload the page
            }
        });     
    } else {
        alert("Dere har allerede valgt 3 prosjekter!");             // display warning if to many project is choosen
    }
}

/*
 * this function removes a request of a project for a group
 */
function removeApprovedProject(projectId, groupId) {    // take project id and group id and send them in post to database query
    $.ajax ({
        url : 'higface/student/library/removeRequest.php',
        type : 'POST',
        data: {
            projectid: projectId,
            groupid: groupId
        },
        success : function (tmp) {
            data = eval ('('+tmp+')');
            if(data.error){             // if something went wrong display error and return
                alert(data.error);
                return;
            }            // if success then reload page
            $('body > section').load ('pages/availableProjects.php');
        }
    });   
}

/*
 * This function updates a request to another priority, executed by drag and drop
 */
function updatePriority() {
    var list = document.getElementById('ourProjects');  // get the list of project request
    var listItems = list.getElementsByTagName('li');    // get all project request
    for(i=0; i<listItems.length; i++){                  // sets the priority
        setPriority(listItems[i].id, listItems[i].value);
    }

}

/*
 * This function sends the priority and id to a database query
 */
function setPriority(id, prio) {
    $.ajax ({
        url : 'higface/student/library/updateRequest.php',
        type : 'POST',
        data: {
            projects: id,
            priority: prio
        },
        success : function (tmp) {            
            data = eval ('('+tmp+')');
            if(data.error){
                alert(data.error);      // if something went wrong display error and return
                return;
            }
        }
    });
}

// kode hentet fra http://www.html5rocks.com/en/tutorials/dnd/basics/#toc-dataTransfer

var dragSrcEl = null;

function handleDragStart(e) {
    // Target (this) element is the source node.

    dragSrcEl = this;

    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.innerHTML);
}

function handleDragOver(e) {
    if (e.preventDefault) {
        e.preventDefault(); // Necessary. Allows us to drop.
    }

    e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.

    return false;
}

function handleDragEnter(e) {
    // this / e.target is the current hover target.
    this.classList.add('over');
}

function handleDragLeave(e) {
    this.classList.remove('over');  // this / e.target is previous target element.
}

function handleDrop(e) {
    // this/e.target is current target element.

    if (e.stopPropagation) {
        e.stopPropagation(); // Stops some browsers from redirecting.
    }

    // Don't do anything if dropping the same column we're dragging.
    if (dragSrcEl != this) {
        // Set the source column's HTML to the HTML of the columnwe dropped on.
        var temp = dragSrcEl.id;                                                //modifyed!!!
        dragSrcEl.id = this.id;                                                 //copy over the project id
        this.id = temp;                                                         // so the set priority sets the right 
        dragSrcEl.innerHTML = this.innerHTML;                                   // priority to right project
        this.innerHTML = e.dataTransfer.getData('text/html');
    }    
    updatePriority();
    return false;
}

function handleDragEnd(e) {
    // this/e.target is the source node.      
    [].forEach.call(cols, function (col) {
        col.classList.remove('over');
    });  
}